import { getDetailAPI, postAddToCartAPI } from "@/api";

const state = {
	detailList: {},
};
const actions = {
	async getDetailList({ commit }, skuId) {
		const res = await getDetailAPI(skuId);
		commit("GETDetailList", res.data);
		// console.log(res.data);
	},
	// mock备用 没有参数
	/* 	async getDetailList({ commit }) {
		const res = await getDetailAPI();
		commit("GETDetailList", res.data.data);
	}, */

	async postCart(context, { skuId, skuNum }) {
		const res = await postAddToCartAPI(skuId, skuNum);
		if (res.code === 200) {
			return "ok";
		} else {
			return "fail";
		}
	},
	/* 	postCart(context, { skuId, skuNum }) {
		return postAddToCartAPI(skuId, skuNum);
	}, */
};
const mutations = {
	GETDetailList(state, data) {
		state.detailList = data;
	},
};

const getters = {
	categoryView(state) {
		return state.detailList.categoryView || {};
	},
	skuInfo(state) {
		return state.detailList.skuInfo || {};
	},
	spuSaleAttrList(state) {
		return state.detailList.spuSaleAttrList || [];
	},
};
export default {
	namespaced: true,
	state,
	actions,
	mutations,
	getters,
};
